/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Simple particle system effects used in the game 
 */

#include "GameEffects.h"
#include "../../Common.h"
#include "../../Resources.h"

using namespace cocos2d;

// small explosion
cocos2d::CCParticleSystem *GameEffects::createSmallExplosion( float x, float y ) {
	CCParticleSystem *mEmitter = cocos2d::CCParticleExplosion::node();
	mEmitter->setIsAutoRemoveOnFinish( true );
	mEmitter->setTexture( cocos2d::CCTextureCache::sharedTextureCache()->addImage( getResource( Textures::GAME_MISC_CARTOONFIRE ) ) );
	mEmitter->setPositionType( kCCPositionTypeRelative );
	mEmitter->setPosition( x, y );
	mEmitter->setPosVar( ccp( WIN_WIDTH * 0.005f, WIN_HEIGHT * 0.005f ) );
	mEmitter->setTotalParticles( 50 );
	mEmitter->setEmissionRate( 1000000 );
	mEmitter->setSpeed( 120.0f );
	mEmitter->setSpeedVar( 70.0f );
	mEmitter->setGravity( ccp( 0.0f, -150.0f ) );
	mEmitter->setLife( 0.7f );
	mEmitter->setLifeVar( 0.2f );
	mEmitter->setAngle( 0 );
	mEmitter->setAngleVar( 180 );
	mEmitter->setStartSize( 20.0f );
	mEmitter->setStartSizeVar( 5.0f );
	mEmitter->setEndSize( 100.0f );
	mEmitter->setEndSizeVar( 20.0f );
	mEmitter->setDuration( 0.2f );
	mEmitter->setIsBlendAdditive( true );
	mEmitter->setScale( RATEX );
	return mEmitter;
}

// medium explosion
cocos2d::CCParticleSystem *GameEffects::createMediumExplosion( float x, float y ) {
	CCParticleSystem *mEmitter = cocos2d::CCParticleExplosion::node();
	mEmitter->setIsAutoRemoveOnFinish( true );
	mEmitter->setTexture( cocos2d::CCTextureCache::sharedTextureCache()->addImage( getResource( Textures::GAME_MISC_CARTOONFIRE ) ) );
	mEmitter->setPositionType( kCCPositionTypeRelative );
	mEmitter->setPosition( x, y );
	mEmitter->setPosVar( ccp( WIN_WIDTH * 0.01f, WIN_HEIGHT * 0.01f ) );
	mEmitter->setTotalParticles( 100 );
	mEmitter->setEmissionRate( 1000000 );
	mEmitter->setSpeed( 110.0f );
	mEmitter->setSpeedVar( 100.0f );
	mEmitter->setGravity( ccp( 0.0f, -150.0f ) );
	mEmitter->setLife( 0.9f );
	mEmitter->setLifeVar( 0.2f );
	mEmitter->setAngle( 0 );
	mEmitter->setAngleVar( 180 );
	mEmitter->setStartSize( 30.0f );
	mEmitter->setStartSizeVar( 5.0f );
	mEmitter->setEndSize( 150.0f );
	mEmitter->setEndSizeVar( 20.0f );
	mEmitter->setDuration( 0.2f );
	mEmitter->setIsBlendAdditive( true );
	mEmitter->setScale( RATEX );
	return mEmitter;
}

// breaking effect when something breaks
cocos2d::CCParticleSystem *GameEffects::createBreakingParticles( float x, float y, char *texture, unsigned int amountOfParticles ) {
	CCParticleSystem *mEmitter = cocos2d::CCParticleExplosion::node();
	mEmitter->setIsAutoRemoveOnFinish( true );
	mEmitter->setTexture( cocos2d::CCTextureCache::sharedTextureCache()->addImage( getResource( texture ) ) );
	mEmitter->setPositionType( kCCPositionTypeRelative );
	mEmitter->setPosition( x, y );
	mEmitter->setPosVar( ccp( WIN_WIDTH * 0.005f, WIN_HEIGHT * 0.005f ) );
	mEmitter->setTotalParticles( amountOfParticles );
	mEmitter->setEmissionRate( 1000000 );
	mEmitter->setSpeed( 300.0f );
	mEmitter->setSpeedVar( 100.0f );
	mEmitter->setGravity( ccp( 0.0f, -WIN_HEIGHT / 2.0f ) );
	mEmitter->setLife( 1.0f );
	mEmitter->setLifeVar( 0.4f );
	mEmitter->setAngle( 0 );
	mEmitter->setAngleVar( 180 );
	mEmitter->setStartSpin( 0 );
	mEmitter->setStartSpinVar( 720 );
	mEmitter->setEndSpin( 0 );
	mEmitter->setEndSpinVar( 720 );
	mEmitter->setStartSize( 10.0f );
	mEmitter->setStartSizeVar( 5.0f );
	mEmitter->setEndSize( 10.0f );
	mEmitter->setEndSizeVar( 5.0f );
	mEmitter->setDuration( 0.2f );
	mEmitter->setIsBlendAdditive( true );
	cocos2d::ccBlendFunc tmp = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
	mEmitter->setBlendFunc( tmp );
	mEmitter->setScale( RATEX );
	/*cocos2d::ccColor4F beginColor = { 1.0f, 1.0f, 1.0f, 1.0f };
	mEmitter->setStartColor( beginColor );
	cocos2d::ccColor4F endColor = { 1.0f, 1.0f, 1.0f, 0.0f };
	mEmitter->setEndColor( endColor );*/
	return mEmitter;
}

// something dismissing or appearing
cocos2d::CCParticleSystem *GameEffects::createSmokeEffect( float x, float y ) {
	CCParticleSystem *mEmitter = cocos2d::CCParticleSmoke::node();
	mEmitter->setEmitterMode( kCCParticleModeGravity );
	mEmitter->setIsAutoRemoveOnFinish( true );
	mEmitter->setTexture( cocos2d::CCTextureCache::sharedTextureCache()->addImage( getResource( Textures::GAME_MISC_DUST ) ) );
	mEmitter->setScale( RATEX * 0.75f );
	mEmitter->setPositionType( kCCPositionTypeRelative );
	mEmitter->setDuration( 0.1f );
	mEmitter->setEmissionRate( 2000 );
	mEmitter->setTotalParticles( 30 );
	mEmitter->setPosition( x, y );
	mEmitter->setPosVar( ccp( WIN_WIDTH / 30.0f, WIN_HEIGHT / 30.0f ) );
	mEmitter->setEndSize( 65 );
	mEmitter->setEndSizeVar( 8 );
	mEmitter->setStartSize( 65 );
	mEmitter->setStartSizeVar( 8 );
	mEmitter->setLife( 1.5f );
	mEmitter->setLifeVar( 0.25f );
	mEmitter->setIsBlendAdditive( false );
	cocos2d::ccBlendFunc tmp = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA };
	mEmitter->setBlendFunc( tmp );
	mEmitter->setAngle( 0 );
	mEmitter->setAngleVar( 360 );
	mEmitter->setGravity( ccp( 0, 0 ) );
	mEmitter->setSpeed( 30 );
	mEmitter->setSpeedVar( 20);
	cocos2d::ccColor4F beginColor = { 140.0f, 140.0f, 140.0f, 1.0f };
	cocos2d::ccColor4F beginColorVar = { 5.0f, 5.0f, 5.0f, 0.0f };
	mEmitter->setStartColor( beginColor );
	mEmitter->setStartColorVar( beginColorVar );
	cocos2d::ccColor4F endColor = { 0.0f, 0.0f, 0.0f, 0.0f };
	cocos2d::ccColor4F endColorVar = { 0.0f, 0.0f, 0.0f, 0.0f };
	mEmitter->setEndColor( endColor );
	mEmitter->setEndColorVar( endColorVar );
	return mEmitter;
}

// create a smoke effect for the background layers
cocos2d::CCParticleSystem *GameEffects::createBackroundSmokeEffect( float x, float y, float wind ) {
	CCParticleSystem *mEmitter = cocos2d::CCParticleSmoke::node();
	mEmitter->setEmitterMode( kCCParticleModeGravity );
	mEmitter->setIsAutoRemoveOnFinish( true );
	mEmitter->setTexture( cocos2d::CCTextureCache::sharedTextureCache()->addImage( getResource( Textures::GAME_MISC_DUST ) ) );
	mEmitter->setScale( RATEX );
	mEmitter->setPositionType( kCCPositionTypeRelative );
	mEmitter->setEmissionRate( 10 );
	mEmitter->setTotalParticles( 100 );
	mEmitter->setPosition( x, y );
	mEmitter->setPosVar( ccp( WIN_WIDTH * 0.005f, 1 ) );
	mEmitter->setEndSize( 40 );
	mEmitter->setEndSizeVar( 4 );
	mEmitter->setStartSize( 5 );
	mEmitter->setStartSizeVar( 2 );
	mEmitter->setLife( 10.0f );
	mEmitter->setLifeVar( 1.0f );
	mEmitter->setIsBlendAdditive( false );
	mEmitter->setAngle( 85 );
	mEmitter->setAngleVar( 7 );
	mEmitter->setGravity( ccp( wind, 2 ) );
	mEmitter->setSpeed( 6 );
	mEmitter->setSpeedVar( 3 );
	cocos2d::ccColor4F beginColor = { 140.0f, 140.0f, 140.0f, 1.0f };
	cocos2d::ccColor4F beginColorVar = { 5.0f, 5.0f, 5.0f, 0.0f };
	mEmitter->setStartColor( beginColor );
	mEmitter->setStartColorVar( beginColorVar );
	cocos2d::ccColor4F endColor = { 0.0f, 0.0f, 0.0f, 0.0f };
	cocos2d::ccColor4F endColorVar = { 0.0f, 0.0f, 0.0f, 0.0f };
	mEmitter->setEndColor( endColor );
	mEmitter->setEndColorVar( endColorVar );
	return mEmitter;
}

// fire effect for the medium bomb sprite
cocos2d::CCParticleSystem *GameEffects::createFireEffect() {
	CCParticleSystem *mEmitter = cocos2d::CCParticleExplosion::node();
	mEmitter->setIsAutoRemoveOnFinish( true );
	mEmitter->setTexture( cocos2d::CCTextureCache::sharedTextureCache()->addImage( getResource( Textures::GAME_MISC_SPARKLE ) ) );
	mEmitter->setPosVar( ccp( 3, 3 ) );
	mEmitter->setDuration( -1 );
	mEmitter->setSpeed( 20.0f );
	mEmitter->setSpeedVar( 5.0f );
	mEmitter->setTotalParticles( 25 );
	mEmitter->setEmissionRate( 125 );
	mEmitter->setGravity( ccp( 0.0f, 0.0f ) );
	mEmitter->setLife( 0.2f );
	mEmitter->setLifeVar( 0.1f );
	mEmitter->setAngle( 0 );
	mEmitter->setAngleVar( 180 );
	mEmitter->setStartSize( 6 );
	mEmitter->setEndSize( 3 );
	mEmitter->setIsBlendAdditive( true );
	mEmitter->setScale( RATEX );
	mEmitter->setPositionType( kCCPositionTypeRelative );
	cocos2d::ccColor4F beginColor = { 0.9f, 0.9f, 0.9f, 1.0f };
	cocos2d::ccColor4F colorVar = { 0.1f, 0.1f, 0.1f, 0.0f };
	mEmitter->setStartColor( beginColor );
	mEmitter->setStartColorVar( colorVar );
	cocos2d::ccColor4F endColor = { 0.0f, 0.9f, 0.0f, 0.0f };
	mEmitter->setEndColor( endColor );
	mEmitter->setEndColorVar( colorVar );
	return mEmitter;
}
